﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;
using System.Globalization;

namespace service_desk_blred.Utils
{
    public enum ResponseEntity
    {
        Sucesso,
        Erro
    }

    public class ResponseInfo
    {
        public ResponseEntity code { get; set; }
        public String descricao { get; set; }
        public Object objResponse { get; set; }

        public ResponseInfo()
        {

        }

        public ResponseInfo(ResponseEntity code)
        {
            this.code = code;
        }

        public ResponseInfo(ResponseEntity code, String descricao)
        {
            this.code = code;
            this.descricao = descricao;

        }


        public ResponseInfo(ResponseEntity code, String descricao, Object obj)
        {
            this.code = code;
            this.descricao = descricao;
            this.objResponse = obj;

        }

        public Boolean isSuccess()
        {
            if (code.Equals(ResponseEntity.Sucesso))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Boolean isError()
        {
            if (code.Equals(ResponseEntity.Erro))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }

    public class ResponseUtils
    {
        public static string RetornaMensagem(ResponseEntity codigo)
        {
            switch (codigo)
            {
                case ResponseEntity.Sucesso:
                    return "Sucesso";
                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please Usuarioct your system administrator.";
            }
        }
    }

    #region Validation

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class PropertiesMustMatchAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
        private readonly object _typeId = new object();

        public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
            : base(_defaultErrorMessage)
        {
            OriginalProperty = originalProperty;
            ConfirmProperty = confirmProperty;
        }

        public string ConfirmProperty { get; private set; }
        public string OriginalProperty { get; private set; }

        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                OriginalProperty, ConfirmProperty);
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
            object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
            return Object.Equals(originalValue, confirmValue);
        }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class ValidatePasswordLengthAttribute : ValidationAttribute
    {

        private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
        private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength;

        public ValidatePasswordLengthAttribute()
            : base(_defaultErrorMessage)
        {
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                name, _minCharacters);
        }

        public override bool IsValid(object value)
        {
            string valueAsString = value as string;
            return (valueAsString != null && valueAsString.Length >= _minCharacters);
        }
    }
    #endregion

}